package net.learn.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.learn.framework.common.page.PageResult;
import net.learn.framework.common.utils.Result;
import net.learn.system.entity.SysUserEntity;
import net.learn.system.entity.SysVideoDetailEntity;
import net.learn.system.entity.SysVideoResultEntity;
import net.learn.system.query.SysVideoQuery;
import net.learn.system.query.SysVideoResultQuery;
import net.learn.system.service.SysUserService;
import net.learn.system.service.SysVideoResultService;
import net.learn.system.vo.SysRadioVO;
import net.learn.system.vo.SysVideoResultVO;
import net.learn.system.vo.SysVideoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微课记录模块
 */
@RestController
@RequestMapping("sys/videoresult")
@Tag(name="微课记录模块")
@AllArgsConstructor
public class SysVideoResultController {
    @Autowired
    SysVideoResultService videoResultService;

    @Autowired
    SysUserService userService;

    @GetMapping("getVideoResult")
    @Operation(summary = "分页获微课记录列表")
    public Result<PageResult<SysVideoResultVO>> getVideoResult(@Valid SysVideoResultQuery query){
        PageResult<SysVideoResultVO> page = videoResultService.qryVideoResultListPage(query);
        return Result.ok(page);
    }
    @PostMapping("/updateVideoResult")
    @Operation(summary = "修改微课记录")
    public Result<String> updateVideo(@RequestBody @Valid SysVideoResultVO vo){
        videoResultService.updateVideoResult(vo);

        return Result.ok();
    }
    @PostMapping("getVideoResultList")
    @Operation(summary = "获取微课记录列表")
    public Result<List<SysVideoResultVO>> getVideoList(@RequestBody @Valid SysVideoResultQuery query){
        List<SysVideoResultVO> list = videoResultService.qryVideoResultList(query);
        return Result.ok(list);
    }
    @PostMapping("save")
    @Operation(summary = "新增微课记录")
    public Result<String> save(@RequestBody SysVideoResultVO qry){
        QueryWrapper<SysVideoResultEntity> videowrapper = new QueryWrapper<>();
        videowrapper.eq("user_id",qry.getUserId());
        videowrapper.eq("detail_id",qry.getDetailId());
        List<SysVideoResultEntity> detailList=videoResultService.list(videowrapper);
        if(detailList.size()>0){
            for (SysVideoResultEntity list:detailList){

                if (detailList.size()>0){;
                    SysVideoResultVO detQry=new SysVideoResultVO();
                    detQry.setId(list.getId().toString());
                    detQry.setViewLen(qry.getViewLen());
                    videoResultService.updateVideoResult(detQry);
                }else {
                    qry.setId("");
                    videoResultService.addVideoResult(qry);
                }
            }
        }
        else{

            videoResultService.addVideoResult(qry);
        }
        return Result.ok();
    }
    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> deleteById(@RequestBody List<Long> idList){
        videoResultService.delete(idList);
        return Result.ok();
    }
}

